package com.hospital.service.publics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.project.entity.ProInfoBase;
import com.hospital.service.project.entity.help.ProInfoBaseHelp;
import com.hospital.service.publics.entity.help.PubFilePathHelp;
import com.hospital.service.publics.model.param.PubFilePathSaveParam;
import common.core.constant.Constant;
import common.core.util.CommonUtil;
import common.core.util.CustomizeRuntimeException;
import common.core.util.FileUtil;
import enhance.plus.logic.LogicBaseServiceImpl;
import com.hospital.service.publics.entity.PubFilePath;
import com.hospital.service.publics.mapper.PubFilePathMapper;
import com.hospital.service.publics.service.PubFilePathService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

/**
* 文件路径表 接口
*
* @author xhl
* @created 2020-11-10 18:42:35
*/
@Service
public class PubFilePathServiceImpl extends LogicBaseServiceImpl<PubFilePathMapper, PubFilePath> implements PubFilePathService{

    private String resourcePath = FileUtil.localPath;

    @Value("${file.fileupload}")
    private String fileTruePath;

    @Override
    public String getFilePath(String superNumber) {
        if (CommonUtil.isEmpty(superNumber)) {
            return null;
        }
        QueryWrapper<PubFilePath> query = PubFilePathHelp.createQuery();
        query.eq(PubFilePathHelp.pathId, superNumber);
        PubFilePath pubFilePath = this.getOne(query);
        if (CommonUtil.isEmpty(pubFilePath)) {
            return null;
        }
        return pubFilePath.getPath();
    }

    @Override
    public void save(PubFilePathSaveParam param) {
        this.save(CommonUtil.copyBean(param, PubFilePath.class));
    }

    @Override
    public String fileCreate(String currentNumber, String superNumber) {
        // 得到夫工程文件目录
        String superFilePath = this.getFilePath(superNumber);
        File file = new File(resourcePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (CommonUtil.isEmpty(superFilePath)) {
            superFilePath = "";
        } else {
            superFilePath += "/";
        }

        File file1 = new File(file, superFilePath + currentNumber);
        file1.mkdirs();
        mkdirsFixSoftFile(file1);
        PubFilePathSaveParam param = new PubFilePathSaveParam();
        param.setPathId(currentNumber);
        param.setPath(superFilePath + currentNumber);
        this.save(param);
        return fileTruePath + "/" + superFilePath + currentNumber + "/RESULT";
    }

    @Override
    public void deleteFile(String projectNumbers) {
        QueryWrapper<PubFilePath> query = PubFilePathHelp.createQuery();
        query.eq(PubFilePathHelp.pathId, projectNumbers);
        this.remove(query);
        FileUtil.deleteFile(new File(resourcePath + File.separatorChar + projectNumbers));
    }

    private void mkdirsFixSoftFile(File file) {
        File f = new File(file, Constant.FILE_NAME_SOFT);
        File f2 = new File(file, Constant.FILE_NAME_TXT);
        File f3 = new File(file, Constant.FILE_NAME_SUPPLEMENTARY);
        File f4 = new File(file, Constant.FILE_NAME_MINIML);
        File f5 = new File(file, Constant.FILE_NAME_RESULT);
        if (!f.exists()) {
            f.mkdirs();
        }
        if (!f2.exists()) {
            f2.mkdirs();
        }
        if (!f3.exists()) {
            f3.mkdirs();
        }
        if (!f4.exists()) {
            f4.mkdirs();
        }
        if (!f5.exists()) {
            f5.mkdirs();
        }
    }
}
